package auto.app.controller;

import auto.app.aop.RuleValidate;
import auto.app.controller.from.InterfaceAddFrom;
import auto.app.controller.from.InterfaceDeleteFrom;
import auto.app.controller.from.InterfacePageFrom;
import auto.app.controller.from.InterfaceUpdateFrom;
import auto.app.controller.info.InterfaceInfo;
import auto.app.controller.info.InterfacePageInfo;
import auto.app.dao.InterfaceDao;
import auto.app.exetion.HibernateExetion;
import auto.app.exetion.MainExection;
import auto.app.model.Interface;
import auto.app.service.InterfaceService;
import auto.app.util.CommonUtil;
import auto.app.util.ExcelUtils;
import auto.app.util.Result;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.ResponseErrorHandler;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static auto.app.exetion.MainExetionMessage.INTERFACEERROR;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-07-09
 */
@RestController
@RequestMapping("/interface")
@Api(value = "接口管理", tags = "接口管理")
@AllArgsConstructor
public class InterfaceController {

    ExcelUtils excelUtils;
    InterfaceService interfaceService;
    InterfaceDao interfaceDao;

    @PostMapping(value = "/add", name = "新增接口")
    @ApiOperation(value = "新增接口", notes = "新增接口")
    @RuleValidate
    public Result<Boolean> addInterface(@Valid @RequestBody InterfaceAddFrom interfaceAddFrom) throws MainExection {
        Boolean aBoolean = interfaceService.addInterface(interfaceAddFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/list", name = "接口列表")
    @ApiOperation(value = "接口列表", notes = "接口列表")
    @RuleValidate
    public Result<InterfacePageInfo> listInterface(@RequestBody InterfacePageFrom interfacePageFrom) {
        InterfacePageInfo interfacePageInfo = interfaceService.listInterface(interfacePageFrom);
        return Result.getInstance(InterfacePageInfo.class).setData(interfacePageInfo);
    }

    @PostMapping(value = "/update", name = "修改接口")
    @ApiOperation(value = "修改接口", notes = "修改接口")
    @RuleValidate
    public Result<Boolean> updateInterface(@Valid @RequestBody InterfaceUpdateFrom interfaceUpdateFrom) throws MainExection {
        Boolean aBoolean = interfaceService.updateInterface(interfaceUpdateFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/delete", name = "删除接口")
    @ApiOperation(value = "删除接口", notes = "删除接口")
    @RuleValidate
    public Result<Boolean> deletes(@Valid @RequestBody InterfaceDeleteFrom interfaceDeleteFrom) throws MainExection {
        Boolean deleteids = interfaceService.deleteids(interfaceDeleteFrom);
        return Result.getInstance(Boolean.class).setData(deleteids);
    }

    //查询详情
    @GetMapping(value = "/detail/{interfaceId}", name = "查询接口详情")
    @ApiOperation(value = "查询接口详情", notes = "查询接口详情")
    @RuleValidate
    public Result<Interface> detail(@PathVariable Integer interfaceId) throws MainExection {
        Interface interfaceServiceById = interfaceService.getById(interfaceId);
        if (ObjectUtil.isNull(interfaceServiceById)) {
            throw new MainExection(INTERFACEERROR.getCode(), INTERFACEERROR.getMsg());
        }
        return Result.getInstance(Interface.class).setData(interfaceServiceById);
    }

    //导出
    @GetMapping(value = "/export", name = "接口管理导出")
    @ApiOperation(value = "接口管理导出", notes = "接口管理导出")
//    @RuleValidate
    public void export(HttpServletResponse response) throws IOException {
        List<InterfaceInfo> interfaceInfoList = interfaceDao.listAllExcel();
        excelUtils.exportExcel("接口列表" + LocalDateTime.now() + ".xls", InterfaceInfo.class, interfaceInfoList, response, "接口列表", true);
    }

    //参数类型
    @GetMapping(value = "/parameterType", name = "接口请求类型")
    @ApiOperation(value = "接口请求类型", notes = "接口请求类型")
    public Result<List<String>> parameterType() {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("from");
        arrayList.add("json");
        return Result.getInstanceList(String.class).setData(arrayList);
    }

    //请求方式
    @GetMapping(value = "/requsetMethod", name = "接口请求方式")
    @ApiOperation(value = "请求方式", notes = "请求方式")
    public Result<List<String>> requsetMethod() {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("GET");
        arrayList.add("HEAD");
        arrayList.add("POST");
        arrayList.add("PUT");
        arrayList.add("PATCH");
        arrayList.add("DELETE");
        arrayList.add("OPTIONS");
        arrayList.add("TRACE");
        return Result.getInstanceList(String.class).setData(arrayList);
    }
}

